// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using Microsoft.MixedReality.Toolkit.Input;
using Microsoft.MixedReality.Toolkit.Utilities;
using Microsoft.MixedReality.Toolkit.Windows.Utilities;
using Microsoft.MixedReality.Toolkit.XRSDK.Input;
using Microsoft.MixedReality.Toolkit.XRSDK;
using Microsoft.MixedReality.Toolkit;

using Unity.Profiling;
using System;
using UnityEngine;
using UnityEngine.XR;


namespace Geo.MRTK.UnityXR
{
    /// <summary>
    /// Manages XR SDK devices on the Windows Mixed Reality platform.
    /// </summary>
    [MixedRealityDataProvider(
        typeof(IMixedRealityInputSystem),
        SupportedPlatforms.Android,
        "UnityXR Device Manager",
        supportedUnityXRPipelines: SupportedUnityXRPipelines.XRSDK)]
    public class UnityXRDeviceManager : XRSDKDeviceManager
    {
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="inputSystem">The <see cref="Microsoft.MixedReality.Toolkit.Input.IMixedRealityInputSystem"/> instance that receives data from this provider.</param>
        /// <param name="name">Friendly name of the service.</param>
        /// <param name="priority">Service priority. Used to determine order of instantiation.</param>
        /// <param name="profile">The service's configuration profile.</param>
        public UnityXRDeviceManager(
            IMixedRealityInputSystem inputSystem,
            string name = null,
            uint priority = DefaultPriority,
            BaseMixedRealityProfile profile = null) : base(inputSystem, name, priority, profile) { }

        protected override Type GetControllerType(SupportedControllerType supportedControllerType)
        {
            return typeof(Microsoft.MixedReality.Toolkit.XRSDK.Oculus.Input.OculusXRSDKTouchController);
        }

        protected override SupportedControllerType GetCurrentControllerType(InputDevice inputDevice)
        {
            return SupportedControllerType.OculusTouch;
        }
        // private static readonly ProfilerMarker GetOrAddControllerPerfMarker = new ProfilerMarker("[MRTK] XRSDKDeviceManager.GetOrAddController");

        // protected override GenericXRSDKController GetOrAddController(InputDevice inputDevice)
        // {
        //     using (GetOrAddControllerPerfMarker.Auto())
        //     {
        //         // If a device is already registered with the ID provided, just return it.
        //         if (ActiveControllers.ContainsKey(inputDevice))
        //         {
        //             var controller = ActiveControllers[inputDevice];
        //             Debug.Assert(controller != null);
        //             return controller;
        //         }

        //         Handedness controllingHand;

        //         if (inputDevice.characteristics.IsMaskSet(InputDeviceCharacteristics.Left))
        //         {
        //             controllingHand = Handedness.Left;
        //         }
        //         else if (inputDevice.characteristics.IsMaskSet(InputDeviceCharacteristics.Right))
        //         {
        //             controllingHand = Handedness.Right;
        //         }
        //         else
        //         {
        //             controllingHand = Handedness.None;
        //         }

        //         SupportedControllerType currentControllerType = GetCurrentControllerType(inputDevice);
        //         Type controllerType = GetControllerType(currentControllerType);

        //         if (controllerType == null)
        //         {
        //             return null;
        //         }

        //         InputSourceType inputSourceType = GetInputSourceType(currentControllerType);

        //         IMixedRealityPointer[] pointers = RequestPointers(currentControllerType, controllingHand);
        //         IMixedRealityInputSource inputSource = Service?.RequestNewGenericInputSource($"{currentControllerType} Controller {controllingHand}", pointers, inputSourceType);
        //         GenericXRSDKController detectedController = Activator.CreateInstance(controllerType, TrackingState.NotTracked, controllingHand, inputSource, null, null) as GenericXRSDKController;

        //         if (detectedController == null || !detectedController.Enabled)
        //         {
        //             // Controller failed to be set up correctly.
        //             Debug.LogError($"Failed to create {controllerType.Name} controller");

        //             // Return null so we don't raise the source detected.
        //             return null;
        //         }

        //         for (int i = 0; i < detectedController.InputSource?.Pointers?.Length; i++)
        //         {
        //             detectedController.InputSource.Pointers[i].Controller = detectedController;
        //         }

        //         ActiveControllers.Add(inputDevice, detectedController);

        //         Service?.RaiseSourceDetected(detectedController.InputSource, detectedController);

        //         return detectedController;
        //     }
        // }
    }
}
